package daoImpls;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import vo.Order;
import vo.PetObject;

public class OrderDAOImpl<E> extends ObjectDAOImpl {
    @Override
    public void add(PetObject hero) {
        Order newOrder =(Order) hero;
        try(Connection c = cp.getConnection(); 
        		PreparedStatement s = c.prepareStatement("insert into `order` values(null,?,?,?,?)")) {
                s.setString(1,newOrder.getUser().getUid());
                s.setString(2,newOrder.getGood().getGid());
                s.setInt(3,newOrder.getoNum());
                s.setInt(4,newOrder.getStatus());
            s.execute();
            cp.returnConnection(c);
        } catch (SQLException e) {
        	 System.out.println(e.getMessage());
        }
    }

    @Override
    public void update(PetObject hero) {
        Order newOrder =(Order) hero;
        try(Connection c = cp.getConnection(); PreparedStatement s =
                c.prepareStatement("update `order` set UID=?,GID=?,ONum=?,status=? where oid = "+ newOrder.getOid())) {
            s.setString(1,newOrder.getUser().getUid());
            s.setString(2,newOrder.getGood().getGid());
            s.setInt(3,newOrder.getoNum());
            s.setInt(4,newOrder.getStatus());
            s.executeUpdate();
            cp.returnConnection(c);
        } catch (SQLException e) {
        	 System.out.println(e.getMessage());
        }
    }

    @Override
    public Order get(String id) {
        Order order = null;
        try(Connection c = cp.getConnection(); 
        		Statement s = c.createStatement()) {
            ResultSet result = s.executeQuery("select * from `order` where oid = " + id);
            if (result.next()){
                order = new Order();
                order.setOid(result.getString(1));
                order.setUser(new UserDAOImpl<>().get(result.getString(2)));
                order.setGood(new GoodDAOImpl<>().get(result.getString(3)));
                order.setoNum(result.getInt(4));
                order.setStatus(result.getInt(5));
            }
            cp.returnConnection(c);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return order;
    }

    public void delete(String id){
        super.delete(id,"oid");
    }
    public void deleteQuery(String id){
    	super.deleteQuery(id, "oid");
    }
	@Override
	public List<PetObject> list() {
		List<PetObject> list = new ArrayList<>();
		String sql = "select * from `order` order by oid ";
		try(Connection c = cp.getConnection();PreparedStatement p = c.prepareStatement(sql)){
			ResultSet rs = p.executeQuery();
			Order order;
			while(rs.next()){
				order = new Order();
				order.setOid(rs.getString(1));
				order.setUser(new UserDAOImpl<>().get(rs.getString(2)));
				order.setGood(new GoodDAOImpl<>().get(rs.getString(3)));
				order.setoNum(rs.getInt(4));
				order.setStatus(rs.getInt(5));
				list.add(order);
			}
			p.close();
			cp.returnConnection(c);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		return list;
	}

	@Override
	public List<PetObject> list(int start, int count) {
		List<PetObject> list = new ArrayList<>();
		String sql = "select * from `order` order by oid desc limit ?,?";
		try(Connection c = cp.getConnection();PreparedStatement p = c.prepareStatement(sql)){
			p.setInt(1, start);
			p.setInt(2, count);
			ResultSet rs = p.executeQuery();
			Order order;
			while(rs.next()){
				order = new Order();
				order.setOid(rs.getString(1));
				order.setUser(new UserDAOImpl<>().get(rs.getString(2)));
				order.setGood(new GoodDAOImpl<>().get(rs.getString(3)));
				order.setoNum(rs.getInt(4));
				order.setStatus(rs.getInt(5));
				list.add(order);
			}
			p.close();
			cp.returnConnection(c);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		return list;
	}
	public List<PetObject> list(int s) {
		List<PetObject> list = new ArrayList<>();
		String sql = "select * from `order` where status=? order by oid desc";
		try(Connection c = cp.getConnection();PreparedStatement p = c.prepareStatement(sql)){
			p.setInt(1, s);
			ResultSet rs = p.executeQuery();
			Order order;
			while(rs.next()){
				order = new Order();
				order.setOid(rs.getString(1));
				order.setUser(new UserDAOImpl<>().get(rs.getString(2)));
				order.setGood(new GoodDAOImpl<>().get(rs.getString(3)));
				order.setoNum(rs.getInt(4));
				order.setStatus(rs.getInt(5));
				list.add(order);
			}
			p.close();
			cp.returnConnection(c);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		return list;
	}
	public List<PetObject> list(String id) {
		List<PetObject> list = new ArrayList<>();
		String sql = "select * from `order` where uid=? order by oid desc";
		try(Connection c = cp.getConnection();PreparedStatement p = c.prepareStatement(sql)){
			p.setString(1, id);
			ResultSet rs = p.executeQuery();
			Order order;
			while(rs.next()){
				order = new Order();
				order.setOid(rs.getString(1));
				order.setUser(new UserDAOImpl<>().get(rs.getString(2)));
				order.setGood(new GoodDAOImpl<>().get(rs.getString(3)));
				order.setoNum(rs.getInt(4));
				order.setStatus(rs.getInt(5));
				list.add(order);
			}
			p.close();
			cp.returnConnection(c);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		return list;
	}
	
	public List<PetObject> listlike(String key) {
		List<PetObject> list = new ArrayList<>();
		String sql = "select * from `order` where uid in (" + key + ") order by oid desc";
		try(Connection c = cp.getConnection();PreparedStatement p = c.prepareStatement(sql)){
			ResultSet rs = p.executeQuery();
			Order order;
			while(rs.next()){
				order = new Order();
				order.setOid(rs.getString(1));
				order.setUser(new UserDAOImpl<>().get(rs.getString(2)));
				order.setGood(new GoodDAOImpl<>().get(rs.getString(3)));
				order.setoNum(rs.getInt(4));
				order.setStatus(rs.getInt(5));
				list.add(order);
			}
			p.close();
			cp.returnConnection(c);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		return list;
	}
}
